package com.zhijian.medical.service.impl;

import com.zhijian.medical.dao.remote.TemperatureRecordMapper;
import com.zhijian.medical.entity.po.TemperatureRecord;
import com.zhijian.medical.entity.vo.request.TemperatureRecordSearchVo;
import com.zhijian.medical.entity.vo.response.TemperatureRecordVo;
import com.zhijian.medical.service.TemperatureRecordService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 温度记录业务处理层
 *
 * @author HCG
 * @version: 1.0
 * @date 2022-03-13 15:06:27
 */
@Slf4j
@Service
public class TemperatureRecordServiceImpl implements TemperatureRecordService {

    @Autowired
    private TemperatureRecordMapper mapper;

    @Override
    public TemperatureRecord selectById(Integer id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public TemperatureRecordVo selectVoById(Integer id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Integer id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public Long insert(TemperatureRecord temperatureRecord) {
        Date now = new Date();
        temperatureRecord.setCreateTime(now);
        temperatureRecord.setUpdateTime(now);
        return mapper.insert(temperatureRecord) > 0 ? temperatureRecord.getId() : 0L;
    }

    @Override
    public boolean update(TemperatureRecord temperatureRecord) {
        Date now = new Date();
        temperatureRecord.setUpdateTime(now);
        return mapper.updateByPrimaryKey(temperatureRecord) != 0;
    }

    @Override
    public Integer count(TemperatureRecordSearchVo temperatureRecordSearchVo) {
        return mapper.count(temperatureRecordSearchVo);
    }

    @Override
    public List<TemperatureRecordVo> selectList(TemperatureRecordSearchVo temperatureRecordSearchVo) {
        return mapper.selectList(temperatureRecordSearchVo);
    }

    @Override
    public Page<TemperatureRecordVo> selectPage(TemperatureRecordSearchVo temperatureRecordSearchVo) {
        //先直接查总数
        int count = count(temperatureRecordSearchVo);
        //分页
        Page<TemperatureRecordVo> page = new Page<>(true, count, temperatureRecordSearchVo.getPageNum(), temperatureRecordSearchVo.getPageSize());
        temperatureRecordSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<TemperatureRecordVo> responseVos = this.selectList(temperatureRecordSearchVo);
        page.setData(responseVos);
        return page;
    }

}